# coding=utf-8
from lxml import etree

# text = ''' <div> <ul>
#         <li class="item-1"><a href="link1.html">first item</a></li>
#         <li class="item-1"><a href="link2.html">second item</a></li>
#         <li class="item-inactive"><a href="link3.html">third item</a></li>
#         <li class="item-1"><a href="link4.html">fourth item</a></li>
#         <li class="item-0"><a href="link5.html">fifth item</a>
#         </ul> </div> '''

text = ''' <div> <ul> 
        <li class="item-1"><a>first item</a></li> 
        <li class="item-1"><a href="link2.html">second item</a></li> 
        <li class="item-inactive"><a href="link3.html">third item</a></li> 
        <li class="item-1"><a href="link4.html">fourth item</a></li> 
        <li class="item-0"><a href="link5.html">fifth item</a> 
        </ul> </div> '''

# 把html字符串转化为element对象，具有xpath方法
html = etree.HTML(text)
# print(html)

# 把element对象转化为字符串
# etree.tostring(html).decode()

# 获取class=item-1下的a的href
href_list = html.xpath("//li[@class='item-1']/a/@href")
# print(href_list)

# 获取class=item-1下的a的文本
title_list = html.xpath("//li[@class='item-1']/a/text()")
# print(title_list)

# 如果每个class为item-1的li标签是1条新闻数据，把这条新闻数据组成一个字典
# for href in href_list:
#     item = {}
#     item["href"] = href
#     item["title"] = title_list[href_list.index(href)]
#     print(item)

# 如果并不是每个a都有href
# 先分组，再进行数据获取
li_list = html.xpath("//li[@class='item-1']")
# print(li_list)
for li in li_list:
    item = {}
    item["href"] = li.xpath("./a/@href")[0] if len(li.xpath("./a/@href")) > 0 else None
    item["title"] = li.xpath("./a/text()")[0] if len(
        li.xpath("./a/text()")) > 0 else None
    print(item)